import React from 'react';

import echarts from 'echarts';

import ReactEcharts from 'echarts-for-react';
import { LineProps } from '../chart.interface';

export class Bar2Chart extends React.Component<LineProps, {}> {
  static defaultProps = {
    categories: [],
    series: [],
  };

  constructor(props: LineProps) {
    super(props);
  }

  prepareOption = (): any => {
    const option = {
      //color: ['#003366', '#006699', '#4cabce', '#e5323e'],
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow',
        },
      },
      grid: {
        top: '5%',
        left: '10%',
        right: '4%',
        bottom: '3%',
      },
      yAxis: [
        {
          type: 'category',
          axisTick: { show: false },
          axisLabel: {
            formatter: (a: any, b: number) => {
              return b + 1;
            },
          },
          data: this.props.categories,
        },
      ],
      xAxis: [
        {
          show: false,
          type: 'value',
        },
      ],
      series: [] as any,
    };

    for (const s of this.props.series!) {
      option.series.push({
        name: s.name,
        type: 'bar',
        label: {
          show: true,
          position: 'insideLeft',
          distance: 15,
          align: 'left',
          formatter: '{b}',
          fontSize: 16,
        },
        showBackground: true,
        backgroundStyle: {
          color: '#1b2431',
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: '#2378f7' },
            { offset: 0.7, color: '#2378f7' },
            { offset: 1, color: '#83bff6' },
          ]),
        },
        emphasis: {
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
              { offset: 0, color: '#83bff6' },
              { offset: 0.5, color: '#188df0' },
              { offset: 1, color: '#188df0' },
            ]),
          },
        },
        data: s.data,
      });
    }
    return option;
  };

  render() {
    return (
      <ReactEcharts option={...this.prepareOption()} style={{ height: '140px', width: '100%' }} />
    );
  }
}
